/**
 @file        Infrasound.VDM.Iterator.h
 @ingroup     libVDM						
 @brief       iterates not-empty blocks in RAM
 @version     $0.1$					
 @since       1.0
 @author      Andreas Schlueter

 

 $Id$
 */


#ifndef VDM_ITERATOR
#define VDM_ITERATOR

#include <ScatterWeb.TypeDefs.h>
#include <Infrasound.VDM.h>
#include "../src/Infrasound.VDM.internals.h"

#ifndef VDM_C
extern STACK(fragmentStack);					///< partly filled blocks
extern STACK(pagedInStack);						///< full and recently paged in blocks
#endif

/**
 * @brief iterator structure
 */
static uint8_t sp_fragments;		///< for the fragmentStack
static uint8_t sp_pagedIn;			///< for the pagedInStack



/**
 * @brief moves initializes iterator and goes to first block containing data
 */
#define iterator_moveFirst() do{\
	dint();\
	_NOP();\
	sp_fragments = fragmentStack.sp-1;\
	sp_pagedIn = pagedInStack.sp-1;\
	eint();\
}while(0);

/**
 * @brief checks if more not-empty blocks exist
 *
 * @return true, if next not-empty block exists
 */
#define iterator_hasNext() ((sp_fragments<=fragmentStack.sp) || (sp_pagedIn<=pagedInStack.sp))

/**
 * @brief returns current not-empty block and moves iterator to next position
 *
 * @return next not-empty block 
 */
#define  iterator_next()\
({\
	sd_block_t* _b = NULL;\
	dint();\
	_NOP();\
	if(sp_fragments<=fragmentStack.sp)\
		_b =	fragmentStack.buf[sp_fragments--];\
	else if(sp_pagedIn<=pagedInStack.sp)\
		_b = pagedInStack.buf[sp_pagedIn--];\
	eint();\
	_b;\
})\


#endif


